﻿<Window xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:primitives="clr-namespace:BoilenEditor.Primitives"
        x:Class="BoilenEditor.SearchWindow"
        DataContext="{Binding RelativeSource={RelativeSource Self}}"
        Width="500"
        ShowInTaskbar="False"
        ResizeMode="NoResize"
        SizeToContent="Height"
        WindowStartupLocation="CenterOwner"
        >
    <Window.CommandBindings>
        <CommandBinding Command="Refresh"
                        CanExecute="SearchCanExecute" />
        <CommandBinding Command="Find"
                        Executed="FindExecuted" />
        <CommandBinding Command="Replace"
                        Executed="ReplaceExecuted" />
        <CommandBinding Command="Close"
                        Executed="CloseExecuted" />
    </Window.CommandBindings>
    <Window.InputBindings>
        <KeyBinding Command="Find"
                    Gesture="Ctrl+F" />
        <KeyBinding Command="Replace"
                    Gesture="Ctrl+H" />
        <KeyBinding Command="Close"
                    Gesture="Ctrl+W" />
    </Window.InputBindings>
    
    <Window.Title>
        <Binding Path="ShowReplace">
            <Binding.Converter>
                <primitives:MatchedValueConverter DefaultResult="Find">
                    <primitives:MatchPair Value="True"
                                          Result="Find and Replace" />
                </primitives:MatchedValueConverter>
            </Binding.Converter>
        </Binding>
    </Window.Title>

    <DockPanel Margin="8">
        <GroupBox DockPanel.Dock="Bottom"
                  Header="Replace Preview"
                  Visibility="Collapsed">
            <TextBox x:Name="preview_"
                     IsReadOnly="True"
                     Background="#EEEEEE" />
        </GroupBox>

        <Grid>
            <Grid.ColumnDefinitions>
                <ColumnDefinition Width="*" />
                <ColumnDefinition Width="16" />
                <ColumnDefinition Width="Auto" />
            </Grid.ColumnDefinitions>

            <StackPanel Grid.Column="0">
                <StackPanel.Resources>
                    <Style TargetType="TextBox">
                        <Setter Property="Margin"
                                Value="0,0,0,8" />
                    </Style>
                    <Style TargetType="CheckBox">
                        <Setter Property="Margin"
                                Value="0,4" />
                    </Style>
                </StackPanel.Resources>

                <Label Content="Search Strin_g:"
                       Target="{Binding ElementName=search_}" />
                <TextBox x:Name="search_"
                         GotFocus="TextBoxGotFocus" />

                <Label Content="Replace wit_h:"
                       Target="{Binding ElementName=replace_}"
                       Visibility="{Binding Path=ShowReplace, Converter={StaticResource BooleanToVisibility}}" />
                <TextBox x:Name="replace_"
                         GotFocus="TextBoxGotFocus"
                         Visibility="{Binding Path=ShowReplace, Converter={StaticResource BooleanToVisibility}}" />

                <CheckBox x:Name="caseSensitive_"
                          Content="Match _case" />
                <CheckBox x:Name="useRegex_"
                          Content="Regular _expression search" />
                <TextBlock Text="(use &quot;{0}&quot; .NET format string syntax for captures)"
                           Margin="22,-4,0,0"
                           Visibility="Collapsed" />
                <CheckBox x:Name="noWrap_"
                          Content="_Don't wrap around" />
            </StackPanel>

            <StackPanel Grid.Column="2"
                        Margin="0,-4,0,0">
                <StackPanel.Resources>
                    <Style TargetType="Button">
                        <Setter Property="Padding"
                                Value="3,4" />
                        <Setter Property="Margin"
                                Value="0,4" />
                    </Style>
                </StackPanel.Resources>

                <Button Content="_Find Next"
                        Command="Refresh"
                        IsDefault="True"
                        Click="FindNext" />
                <Button Content="Find _Previous"
                        Command="Refresh"
                        Click="FindPrevious" />
                <Button Content="_Replace"
                        Command="Refresh"
                        Click="Replace"
                        Visibility="{Binding Path=ShowReplace, Converter={StaticResource BooleanToVisibility}}" />
                <Button Content="Replace _All"
                        Command="Refresh"
                        Click="ReplaceAll"
                        Visibility="{Binding Path=ShowReplace, Converter={StaticResource BooleanToVisibility}}" />
                <Button Content="Close"
                        IsCancel="True"
                        Command="Close" />
            </StackPanel>
        </Grid>
    </DockPanel>

    <FocusManager.FocusedElement>
        <Binding ElementName="search_"
                 Mode="OneTime" />
    </FocusManager.FocusedElement>
</Window>
